#include "ftmessagebox.h"
#include "ftvboxlayout.h"
#include "ftlineedit.h"
#include "fthboxlayout.h"

#include <QtGui>
#include "ftlabel.h"
#include "ftpushbutton.h"
#include "ftcolordialog.h"
#include "fttextarea.h"

FTMessageBox::FTMessageBox(FTMessageBox::type t,
                          const QString& title,
                          const QString& msg,
                          const QString& button1,
                          const QString& button2,
                          const QString& button3,
                          const QColor& color) :
    RotatingDialog()
{
    msgType = t;
    switch ( msgType )
    {
    case FTMessageBox::msg_info:
        buildCommon( title, msg, tr("Ok"), "", "", ":/misc/msg_info.png");
        break;

    case FTMessageBox::msg_error:
        buildCommon( title, msg, tr("Close"), "", "", ":/misc/msg_error.png");
        break;

    case FTMessageBox::msg_yesno:
        buildCommon( title, msg, tr("Yes"), tr("No"), "", ":/misc/msg_question.png");
        break;

    case FTMessageBox::msg_3question:
        buildCommon( title, msg, button1, button2, button3, ":/misc/msg_question.png");
        break;

    case FTMessageBox::msg_color:
        buildCommon( title, msg, tr("Confirm"), tr("Cancel"), "", ":/misc/msg_color.png");
        msg_color_dialog = new FTColorDialog(color);
        break;

    case FTMessageBox::msg_input:
        buildCommon( title, msg, tr("Confirm"), tr("Cancel"), "", ":/misc/msg_input.png");
        msg_input_line = new FTLineEdit( button1 );
        break;

    case FTMessageBox::msg_progress:
        buildCommon( title, msg, tr("Abort"), "", "", ":/misc/msg_info.png");
        msg_progress_bar = new QProgressBar();
        break;
    }
}

FTMessageBox::~FTMessageBox()
{
    foreach (QAbstractButton* btn, buttons.buttons() )
        buttons.removeButton( btn );
}

void FTMessageBox::buildCommon(const QString &title,
                               const QString &msg,
                               const QString &btn1,
                               const QString &btn2,
                               const QString &btn3,
                               const QString &icon)
{
    msg_title = new FTLabel( title );
    msg_title->setAlignment( Qt::AlignVCenter | Qt::AlignHCenter );
    msg_title->setTitle(true);

    msg_icon = QImage( icon );
    msg_icon = msg_icon.scaled( FTControls::adjustSize(msg_icon.size() ) );

    message = new FTTextArea( msg, 2 );
    message->setAlignment( Qt::AlignHCenter | Qt::AlignVCenter );

    button1 = new FTPushButton( btn1 );
    buttons.addButton( button1,1 );
    if ( btn2 != "" )
    {
        button2 = new FTPushButton( btn2 );
        buttons.addButton( button2, 0 );
    }
    else
        button2 = NULL;

    if ( btn3 != "" )
    {
        button3 = new FTPushButton( btn3 );
        buttons.addButton( button3, 2 );
    }
    else
        button3 = NULL;

    connect( &buttons, SIGNAL(buttonClicked(int)),
             this, SLOT(buttonSelected(int)) );

    msg_progress_bar = NULL;
    msg_input_line   = NULL;
    msg_color_dialog = NULL;
}

//void FTMessageBox::enableButtons(bool e)
//{
//    button1->setEnabled(e);
//    if ( button2 != NULL )
//        button2->setEnabled( e );
//    if ( button3 != NULL )
//        button3->setEnabled( e );
//}

void FTMessageBox::paintEvent(QPaintEvent *)
{
    QPainter painter(this);

    int def_height = FTControls::defaultHeight();
    int row_height = msg_title->height() + 1;
    if ( row_height < def_height )
        row_height = def_height;

    painter.drawLine(0, row_height, width(), row_height );
    painter.drawImage(0,0, msg_icon );
}

void FTMessageBox::buttonSelected(int n)
{
    exitLoop(n);
}

void FTMessageBox::updateProgress(qint64 p)
{
    if ( msg_progress_bar != NULL )
        msg_progress_bar->setValue( p );
}

void FTMessageBox::updateProgress(qint64 p, qint64 m)
{
    if ( msg_progress_bar != NULL )
    {
        msg_progress_bar->setMaximum( m );
        msg_progress_bar->setValue( p );
    }
}

QLayout* FTMessageBox::createHorizontal()
{
    return createVertical();
}

QLayout* FTMessageBox::createVertical()
{
    FTVBoxLayout* grid = new FTVBoxLayout();

    grid->addWidget(msg_title);

    grid->addStretch();

    grid->addWidget(message);

    switch ( msgType )
    {
    case FTMessageBox::msg_info:
    case FTMessageBox::msg_error:
    case FTMessageBox::msg_yesno:
    case FTMessageBox::msg_3question:
        grid->addStretch();
        break;

    case FTMessageBox::msg_color:
        grid->addWidget( msg_color_dialog, 2 );
        break;

    case FTMessageBox::msg_input:
        grid->addStretch();
        grid->addWidget( msg_input_line );
        grid->addStretch();
        break;

    case FTMessageBox::msg_progress:
        grid->addStretch();
        grid->addWidget( msg_progress_bar );
        grid->addStretch();
        break;

    }

    FTHBoxLayout* bgrid = new FTHBoxLayout();
    bgrid->addWidget( button1 );
    if ( button2 != NULL )
        bgrid->addWidget( button2 );
    if ( button3 != NULL )
        bgrid->addWidget( button3 );

    grid->addLayout( bgrid );
    return grid;
}

void FTMessageBox::Info(const QString &t,
                        const QString &m,
                        const QString &ok)
{
    FTMessageBox msg( FTMessageBox::msg_info, t, m, ok );
    msg.exec();
}

void FTMessageBox::Error(const QString &t,
                         const QString &m,
                         const QString &ok)
{
    FTMessageBox msg( FTMessageBox::msg_error, t, m, ok );
    msg.exec();
}

bool FTMessageBox::Yesno(const QString &t,
                         const QString &m,
                         const QString &ok,
                         const QString &ko)
{
    FTMessageBox msg( FTMessageBox::msg_yesno, t, m, ok,ko );
    return msg.exec() == 1; // the leftmost button, the "ok" button.
}

int FTMessageBox::Question(const QString &t,
                           const QString &m,
                           const QString &a,
                           const QString& b,
                           const QString &c)
{
    // Insert a and b, so that a = 0, b = 1, c = 2!
    FTMessageBox msg( FTMessageBox::msg_3question, t, m, b,a,c );
    return msg.exec();
}

QColor FTMessageBox::Color(const QString& t,
                           const QString& m,
                           const QColor& c)
{
    FTMessageBox msg( FTMessageBox::msg_color, t, m, "", "", "", c );
    if ( msg.exec() == 1 ) // leftmost button, OK button.
    {
        return msg.msg_color_dialog->currentColor();
    }
    else
        return c;
}

QString FTMessageBox::TextInput(const QString& t,
                                const QString& m,
                                const QString& d)
{
    FTMessageBox msg( FTMessageBox::msg_input, t, m, d );
    if ( msg.exec() == 1 ) // leftmost button, OK button
    {
        return msg.msg_input_line->text();
    }
    return d;
}

FTMessageBox* FTMessageBox::Progress( const QString& t,
                                      const QString& m,
                                      int min,
                                      int max)
{
    FTMessageBox* msg = new FTMessageBox( FTMessageBox::msg_progress, t, m );
    msg->msg_progress_bar->setMinimum( min );
    msg->msg_progress_bar->setMaximum( max );
    msg->msg_progress_bar->setValue(min);
    return msg;
}
